<template>
  <a-modal
    :open="open"
    width="640px"
    :title="rowData?.id ? '修改护理措施模板' : '新增护理措施模板'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form class="form-style-flex" name="validate_other" :model="formState">
      <a-form-item label="模板名称" class="w-full" v-bind="validateInfos.methodName">
        <a-input v-model:value="formState.methodName" placeholder="请输入" @blur="getZjm" />
      </a-form-item>
      <a-form-item label="助记码" class="w-1/2" v-bind="validateInfos.zjm">
        <a-input v-model:value="formState.zjm" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="顺序号" class="w-1/2" v-bind="validateInfos.outOrder">
        <a-input v-model:value="formState.outOrder" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="模板内容" class="w-full" v-bind="validateInfos.methodContent">
        <a-textarea v-model:value="formState.methodContent" :rows="6" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'update:value', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  },
  title: {
    type: String,
    default: ''
  }
})

const formState = ref({
  methodName: '',
  methodContent: '',
  outOrder: '',
  zjm: ''
})
const getZjm = async (e) => {
  const { data, code, msg } = await post('/common/util/getPinyin', {
    name: e.target.value
  })
  if (code !== 0) return message.error(msg)
  formState.value.zjm = data
}

const rulesState = ref({
  methodName: [{ required: true, message: '请输入模板名称' }],
  methodContent: [{ required: true, message: '请输入模板内容' }],
  outOrder: [{ required: true, message: '请输入顺序号' }],
  zjm: [{ required: true, message: '请输入助记码' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.id !== '') {
        formState.value = {
          ...props.rowData
        }
      }
    }
  }
)

const onOk = async () => {
  await validate()
  const res = await post('/inp/InpDictNursingMethod/doSaveInpNursingMethod', formState.value)
  if (res.code === 0) {
    message.success(res.msg)

    emit('success')

    onCancel()
  } else {
    message.error(res.msg)
  }
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
</script>
<style lang="less" scoped></style>
